using FarPoint.Win;
using FarPoint.Win.Spread;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Spread.Sparkline.Features
{
    public partial class ParetoSparkline : Spread.Common.Features.Sample.BaseSampleControl
    {
        public ParetoSparkline()
        {
            InitializeComponent();
            Name = "Pareto";
            InitControls();
            InitHorizontalSparkline(_fpSpread.Sheets[0]);
            IntiVerticalSparkline(_fpSpread.Sheets[1]);
        }

        #region Private Methods
        private void IntiVerticalSparkline(SheetView sheetView)
        {
            var worksheet = sheetView.AsWorksheet();
            worksheet.Name = "Vertical";

            worksheet.SetValue(1, 0, new object[,]{
               { "Reason", "Traffic", "Child care", "Public transportation", "Weather", "Overslept", "Emergency"},
               { "Cases", 20, 15, 13, 5, 4, 1},
               { "Color", "#CCCCCC", "#82BC00", "#F7A711", "#00C2D6", "#FFE7BA", "#000000"},
               { "Bar Size", 0.1, 0.3, 0.5, 0.7, 0.8, 1},
               { "Diagram", null, null, null, null, null, null}
            });

            worksheet.Cells["A9:I9"].Merge();
            worksheet.Cells["A9"].Value = "*Result: By the sparkline above can draw a conclusion that the reasons for 80% of the employees be late are \"traffic\", \"child care\" and \"public transportation\".";

            for (var index = 1; index < 7; index++)
            {
                worksheet.Cells[$"{(char)(index + 65)}6"].Formula2 = $"PARETOSPARKLINE($B$3:$G$3,COLUMN()-1,$B$4:$G$4, " +
                $"{_txtTarget1.Text}, " +
                $"{_txtTarget2.Text},0,2,true," +
                $"\"{_cmbTarget1Color.SelectedItem}\", " +
                $"\"{_cmbTarget2Color.SelectedItem}\",$B$4:$G$4,$B$5:$G$5)";
            }

            for (var i = 0; i < 7; i++)
            {
                worksheet.Columns[$"{(char)(i + 65)}"].ColumnWidth = 120;
            }
            worksheet.Columns["D"].ColumnWidth = 150;
            worksheet.Rows["6"].RowHeight = 160;
            worksheet.Rows["1"].RowHeight = 35;
            worksheet.Cells["A1:G1"].Merge();

            worksheet.Cells["A1"].Value = "Late Arrivals by Reported Cause";
            worksheet.Cells["A1"].VerticalAlignment = GrapeCity.Spreadsheet.VerticalAlignment.Center;
            worksheet.Cells["A1"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 10, Bold = true });
            sheetView.Cells["A1"].BackColor = Color.Gray;
            sheetView.Cells["A1"].ForeColor = Color.White;

            worksheet.Cells["A2:G5"].HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.Left;
            worksheet.Cells["A2:A6"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 10, Bold = true });
            worksheet.Cells["A2:A6"].Borders.ApplyBorder(new GrapeCity.Spreadsheet.Border()
            {
                Right = new GrapeCity.Spreadsheet.BorderLine(GrapeCity.Spreadsheet.BorderLineStyle.Thin, GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Black))
            });
        }

        private void InitHorizontalSparkline(SheetView sheetView)
        {
            var worksheet = sheetView.AsWorksheet();
            worksheet.Name = "Horizontal";

            worksheet.SetValue(1, 0, new object[,] {
                { "Reason", "Cases", "Color", "Bar Size", "Diagram" },
                { "Traffic", 20, "#CCCCCC", 0.1, null },
                { "Child care", 15, "#82BC00", 0.3, null },
                { "Public transportation", 13, "#F7A711", 0.5, null },
                { "Weather", 6, "#00C2D6", 0.7, null },
                { "Overslept", 4, "#FFE7BA", 0.8, null },
                { "Emergency", 1, "#000000", 1, null }
            });

            worksheet.Cells["A11:I11"].Merge();
            worksheet.Cells["A11"].Value = "*Result: By the sparkline above can draw a conclusion that the reasons for 80% of the employees be late are \"traffic\", \"child care\" and \"public transportation\".";

            for (int index = 2; index < 8; index++)
            {
                worksheet.Cells[$"E{index + 1}"].Formula2 = $"PARETOSPARKLINE($B$3:$B$8,ROW()-2,$C$3:$C$8, " +
                    $"{_txtTarget1.Text}, " +
                    $"{_txtTarget2.Text},0,2,false," +
                    $"\"{_cmbTarget1Color.SelectedItem}\", " +
                    $"\"{_cmbTarget2Color.SelectedItem}\",$C$3:$C$8,$D$3:$D$8)";
            }

            for (int i = 1; i < 4; i++)
            {
                worksheet.Columns[$"{(char)(i + 65)}"].ColumnWidth = 80;
            }

            worksheet.Columns["E"].ColumnWidth = 340;
            worksheet.Columns["A"].ColumnWidth = 140;
            worksheet.Rows["1"].RowHeight = 35;
            worksheet.Cells["A1:E1"].Merge();

            worksheet.Cells["A1"].Value = "Late Arrivals by Reported Cause";
            worksheet.Cells["A1"].VerticalAlignment = GrapeCity.Spreadsheet.VerticalAlignment.Center;
            worksheet.Cells["A1"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 10, Bold = true });
            sheetView.Cells["A1"].BackColor = Color.Gray;
            sheetView.Cells["A1"].ForeColor = Color.White;

            worksheet.Cells["A2:D8"].HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.Left;
            worksheet.Cells["A2:E2"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 10, Bold = true });
            worksheet.Cells["A2:E2"].Borders.ApplyBorder(new GrapeCity.Spreadsheet.Border()
            {
                Bottom = new GrapeCity.Spreadsheet.BorderLine(GrapeCity.Spreadsheet.BorderLineStyle.Thin, GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Black))
            });
        }

        private void ApplyChanges()
        {
            var worksheet = _fpSpread.ActiveSheet.AsWorksheet(); ;

            if (_fpSpread.ActiveSheetIndex == 0)
            {
                var formula = $"PARETOSPARKLINE($B$3:$B$8,ROW()-2,$C$3:$C$8," +
                    $"{_txtTarget1.Text}," +
                    $"{_txtTarget2.Text}, " +
                    $"{_cmbHighlightItem.SelectedIndex}, " +
                    $"{_cmbLabelType.SelectedIndex}, false, " +
                    $"\"{_cmbTarget1Color.SelectedItem}\", " +
                    $"\"{_cmbTarget2Color.SelectedItem}\",$C$3:$C$8,$D$3:$D$8)";

                for (int i = 2; i <= 7; i++)
                {
                    worksheet.Cells[$"E{i + 1}"].Formula2 = formula;
                }
            }
            else
            {
                var formulaVertical = $"PARETOSPARKLINE($B$3:$G$3,COLUMN()-1,$B$4:$G$4," +
                    $"{_txtTarget1.Text}, {_txtTarget2.Text}, " +
                    $"{_cmbHighlightItem.SelectedIndex}, " +
                    $"{_cmbLabelType.SelectedIndex}, true, " +
                    $"\"{_cmbTarget1Color.SelectedItem}\", " +
                    $"\"{_cmbTarget2Color.SelectedItem}\",$B$4:$G$4,$B$5:$G$5)";

                for (int index = 1; index < 7; index++)
                {
                    worksheet.Cells[$"{(char)(index + 65)}6"].Formula2 = formulaVertical;
                }
            }
        }

        private void InitControls()
        {
            _cmbLabelType.DataSource = new object[] { "None", "Cumulated Percentage", "Single Percentage" };
            _cmbHighlightItem.DataSource = new string[] { "None", "1st", "2nd", "3rd", "4th", "5th", "6th" };

            _txtTarget1.Text = "0.5";
            _txtTarget2.Text = "0.8";

            _cmbTarget1Color.DataSource = new string[] { "Gray", "Green", "Black", "Orange" };
            _cmbTarget2Color.DataSource = new string[] { "Gray", "Green", "Black", "Orange" };

            _cmbLabelType.DropDownStyle = _cmbHighlightItem.DropDownStyle = _cmbTarget1Color.DropDownStyle = _cmbTarget2Color.DropDownStyle = ComboBoxStyle.DropDownList;

            _txtTarget1.LostFocus += OnTextBoxLostFocus;
            _txtTarget2.LostFocus += OnTextBoxLostFocus;
            _cmbLabelType.SelectedIndexChanged += OnComboBoxSelectionChanged;
            _cmbHighlightItem.SelectedIndexChanged += OnComboBoxSelectionChanged;
            _cmbTarget1Color.SelectedIndexChanged += OnComboBoxSelectionChanged;
            _cmbTarget2Color.SelectedIndexChanged += OnComboBoxSelectionChanged;

            _cmbLabelType.SelectedIndex = 2;
            _cmbHighlightItem.SelectedIndex = 0;
            _cmbTarget1Color.SelectedIndex = 0;
            _cmbTarget2Color.SelectedIndex = 3;
        }

        private void OnComboBoxSelectionChanged(object sender, EventArgs e)
        {
            ApplyChanges();
        }

        private void OnTextBoxLostFocus(object sender, EventArgs e)
        {
            if (double.TryParse((sender as TextBox).Text, out double _))
            {
                ApplyChanges();
            }
        }
        #endregion
    }
}


//
//ParetoSparkline.Designer.cs
//



namespace Spread.Sparkline.Features
{
    partial class ParetoSparkline
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ParetoSparkline));
            this._fpSpread = new FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None, resources.GetObject("resource1"));
            this._fpSpread_Sheet1 = this._fpSpread.GetSheet(0);
            this._fpSpread_Sheet2 = this._fpSpread.GetSheet(1);
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this._cmbTarget2Color = new System.Windows.Forms.ComboBox();
            this._cmbTarget1Color = new System.Windows.Forms.ComboBox();
            this._cmbHighlightItem = new System.Windows.Forms.ComboBox();
            this._cmbLabelType = new System.Windows.Forms.ComboBox();
            this.label6 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this._txtTarget1 = new System.Windows.Forms.TextBox();
            this._txtTarget2 = new System.Windows.Forms.TextBox();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this._fpSpread)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this._fpSpread);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.groupBox1);
            // 
            // _fpSpread
            // 
            this._fpSpread.AccessibleDescription = "Book1, Sheet1, Row 0, Column 0";
            this._fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this._fpSpread.Font = new System.Drawing.Font("Calibri", 11F);
            this._fpSpread.Location = new System.Drawing.Point(0, 0);
            this._fpSpread.Name = "_fpSpread";
            this._fpSpread.Size = new System.Drawing.Size(477, 487);
            this._fpSpread.TabIndex = 0;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this._txtTarget2);
            this.groupBox1.Controls.Add(this._txtTarget1);
            this.groupBox1.Controls.Add(this._cmbTarget2Color);
            this.groupBox1.Controls.Add(this._cmbTarget1Color);
            this.groupBox1.Controls.Add(this._cmbHighlightItem);
            this.groupBox1.Controls.Add(this._cmbLabelType);
            this.groupBox1.Controls.Add(this.label6);
            this.groupBox1.Controls.Add(this.label5);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(157, 271);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Sparkline Settings:";
            // 
            // _cmbTarget2Color
            // 
            this._cmbTarget2Color.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._cmbTarget2Color.FormattingEnabled = true;
            this._cmbTarget2Color.Location = new System.Drawing.Point(92, 232);
            this._cmbTarget2Color.Name = "_cmbTarget2Color";
            this._cmbTarget2Color.Size = new System.Drawing.Size(62, 21);
            this._cmbTarget2Color.TabIndex = 10;
            // 
            // _cmbTarget1Color
            // 
            this._cmbTarget1Color.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._cmbTarget1Color.FormattingEnabled = true;
            this._cmbTarget1Color.Location = new System.Drawing.Point(92, 190);
            this._cmbTarget1Color.Name = "_cmbTarget1Color";
            this._cmbTarget1Color.Size = new System.Drawing.Size(62, 21);
            this._cmbTarget1Color.TabIndex = 9;
            // 
            // _cmbHighlightItem
            // 
            this._cmbHighlightItem.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._cmbHighlightItem.FormattingEnabled = true;
            this._cmbHighlightItem.Location = new System.Drawing.Point(92, 65);
            this._cmbHighlightItem.Name = "_cmbHighlightItem";
            this._cmbHighlightItem.Size = new System.Drawing.Size(62, 21);
            this._cmbHighlightItem.TabIndex = 7;
            // 
            // _cmbLabelType
            // 
            this._cmbLabelType.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._cmbLabelType.FormattingEnabled = true;
            this._cmbLabelType.Location = new System.Drawing.Point(92, 25);
            this._cmbLabelType.Name = "_cmbLabelType";
            this._cmbLabelType.Size = new System.Drawing.Size(62, 21);
            this._cmbLabelType.TabIndex = 6;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(5, 235);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(74, 13);
            this.label6.TabIndex = 5;
            this.label6.Text = "Target2 Color:";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(5, 193);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(74, 13);
            this.label5.TabIndex = 4;
            this.label5.Text = "Target1 Color:";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(6, 153);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(47, 13);
            this.label4.TabIndex = 3;
            this.label4.Text = "Target2:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(6, 110);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(47, 13);
            this.label3.TabIndex = 2;
            this.label3.Text = "Target1:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(6, 68);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(74, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Highlight Item:";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(6, 28);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(63, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Label Type:";
            // 
            // _txtTarget1
            // 
            this._txtTarget1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._txtTarget1.Location = new System.Drawing.Point(92, 107);
            this._txtTarget1.Name = "_txtTarget1";
            this._txtTarget1.Size = new System.Drawing.Size(62, 20);
            this._txtTarget1.TabIndex = 11;
            // 
            // _txtTarget2
            // 
            this._txtTarget2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._txtTarget2.Location = new System.Drawing.Point(92, 150);
            this._txtTarget2.Name = "_txtTarget2";
            this._txtTarget2.Size = new System.Drawing.Size(62, 20);
            this._txtTarget2.TabIndex = 12;
            // 
            // ParetoSparkline
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "ParetoSparkline";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this._fpSpread)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread _fpSpread;
        private FarPoint.Win.Spread.SheetView _fpSpread_Sheet1;
        private FarPoint.Win.Spread.SheetView _fpSpread_Sheet2;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox _cmbTarget2Color;
        private System.Windows.Forms.ComboBox _cmbTarget1Color;
        private System.Windows.Forms.ComboBox _cmbHighlightItem;
        private System.Windows.Forms.ComboBox _cmbLabelType;
        private System.Windows.Forms.TextBox _txtTarget2;
        private System.Windows.Forms.TextBox _txtTarget1;
    }
}
